package com.dmxh.service.impl;

import com.dmxh.mapper.TbGoodsDescMapper;
import com.dmxh.mapper.TbGoodsMapper;
import com.dmxh.mapper.TbItemMapper;
import com.dmxh.params.QueryParams;
import com.dmxh.pojo.*;
import com.dmxh.result.PageList;
import com.dmxh.service.GoodsService;
import com.dmxh.vo.GoodsDescVo;
import com.dmxh.vo.GoodsVo;
import com.dmxh.vo.ItemVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    @Transactional
    public void addInfo(GoodsVo goodsVo) {
        TbGoods tbGoods = new TbGoods();
        TbGoodsDesc tbGoodsDesc = new TbGoodsDesc();
        BeanUtils.copyProperties(goodsVo, tbGoods);
        BeanUtils.copyProperties(goodsVo, tbGoodsDesc);
        tbGoods.setAuditStatus("0");
        tbGoodsMapper.insertForKey(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insertNew(tbGoodsDesc);
    }

    @Override
    public PageList<GoodsVo> queryPageBySeller(QueryParams queryParams) {
        Page<TbGoods> page = PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        List<TbGoods> tbGoods = tbGoodsMapper.selectByExample(null);
        PageInfo<TbGoods> pageInfo = page.toPageInfo();
        List<GoodsVo> collect = tbGoods.stream().map(tbGoods1 -> {
            GoodsVo goodsVo = new GoodsVo();
            BeanUtils.copyProperties(tbGoods1, goodsVo);
            //同过主键依次查询所有详情

            //将所有信息封装进goodsvo
            Optional.ofNullable(tbGoodsDescMapper.selectById(tbGoods1.getId())).ifPresent(tbGoodsDesc1 -> {
                BeanUtils.copyProperties(tbGoodsDesc1,goodsVo);
            });
            return goodsVo;
        }).collect(Collectors.toList());
        return new PageList<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), collect);
    }

    @Override
    @Transactional
    public void updateInfo(GoodsVo goodsVo) {
        TbGoods tbGoods = new TbGoods();
        BeanUtils.copyProperties(goodsVo,tbGoods);
        //将商品状态设置为申请中
        tbGoods.setAuditStatus("1");
        tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        TbGoodsDesc tbGoodsDesc = new TbGoodsDesc();
        BeanUtils.copyProperties(goodsVo, tbGoodsDesc);
        tbGoodsDesc.setGoodsId(goodsVo.getId());
        tbGoodsDescMapper.updateNew(tbGoodsDesc);
    }

    @Override
    public PageList<GoodsVo> queryPageByAdmin(QueryParams queryParams) {
        //创建集合存储运营商查看的商品信息状态码
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        Page<TbGoods> page = PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        TbGoodsExample tbGoodsExample = new TbGoodsExample();
        tbGoodsExample.createCriteria().andAuditStatusIn(strings);
        List<TbGoods> tbGoods = tbGoodsMapper.selectByExample(tbGoodsExample);
        PageInfo<TbGoods> pageInfo = page.toPageInfo();
        List<GoodsVo> collect = tbGoods.stream().map(tbGoods1 -> {
            GoodsVo goodsVo = new GoodsVo();
            BeanUtils.copyProperties(tbGoods1, goodsVo);
            //同过主键依次查询所有详情
            //将所有信息封装进goodsvo
            Optional.ofNullable(tbGoodsDescMapper.selectById(tbGoods1.getId())).ifPresent(tbGoodsDesc -> {
                BeanUtils.copyProperties(tbGoodsDesc,goodsVo);
            });

            return goodsVo;
        }).collect(Collectors.toList());
        return new PageList<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), collect);
    }

    @Override
    public void updateStatus(GoodsVo goodsVo) {
        TbGoods tbGoods = new TbGoods();
        BeanUtils.copyProperties(goodsVo, tbGoods);
        tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
    }

    @Override
    public GoodsVo getById(long id) {
//        TbItemExample tbItemExample = new TbItemExample();
//        tbItemExample.createCriteria().andGoodsIdEqualTo(id);
//        List<TbItem> tbItems = tbItemMapper.selectByExample(tbItemExample);
        TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
        GoodsVo goodsVo = new GoodsVo();
        BeanUtils.copyProperties(tbGoods,goodsVo);
        TbGoodsDesc tbGoodsDesc = tbGoodsDescMapper.selectById(id);
        GoodsDescVo goodsDescVo = new GoodsDescVo();
        BeanUtils.copyProperties(tbGoodsDesc,goodsDescVo);
        goodsVo.setGoodsDescVo(goodsDescVo);
        return goodsVo;
    }

}
